package com.example.module.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.R;
import com.example.module.dialog.LoginLayout.LoginListener;
import com.example.util.StringUtil;

public class LoginDialog extends Dialog {

	private TextView dialogTitle;
	private ImageButton closeButton;
	private LoginLayout passcodeLayout;

	public LoginDialog(Context context, LoginListener loginListener) {
		super(context);
		initializeView(context, "Login");
		initializeAction(context, loginListener);
	}

	private void initializeView(Context context, String title) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dialog_login);
		
		dialogTitle = (TextView) findViewById(R.id.dialogTitle);
		closeButton = (ImageButton) findViewById(R.id.closeButton);
		passcodeLayout = (LoginLayout) findViewById(R.id.loginLayout);
		
		if (StringUtil.isEmpty(title))
			dialogTitle.setVisibility(View.GONE);	
		else
			dialogTitle.setText(title);
        
        DisplayMetrics displayMetrics = new DisplayMetrics();
		((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(displayMetrics);
		Log.d("SimpleDialog.initializeView", "displayMetrics.widthPixels=" + displayMetrics.widthPixels);
		Log.d("SimpleDialog.initializeView", "displayMetrics.heightPixels=" + displayMetrics.heightPixels);
        
        WindowManager.LayoutParams params = getWindow().getAttributes();
        
        params.width = displayMetrics.widthPixels - 0;
        getWindow().setAttributes(params);
        
        getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        setCanceledOnTouchOutside(true);
	}
	
	private void initializeAction(Context context, LoginListener loginListener) {
		closeButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		
		passcodeLayout.setLoginListener(loginListener);
	}
	
	public void showDialog(String email) {
//		String email = PreferenceUtil.getString(getContext(), "Email");
//		String password = PreferenceUtil.getString(getContext(), "Password");
		
//		if (!StringUtil.isEmpty(pEmail) && !pEmail.equals(email)) {
//			email = pEmail;
//			password = "";
//		}
		
		passcodeLayout.setEmail(email);
		passcodeLayout.setPassword("");
		show();
	}

	
}
